package me.stone.stanimeclient.core.data.domain.setting

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import me.stone.stanimeclient.core.common.di.DefaultDispatcher
import me.stone.stanimeclient.core.common.di.IoDispatcher
import me.stone.stanimeclient.core.data.domain.FlowUseCase
import me.stone.stanimeclient.core.data.domain.Result
import me.stone.stanimeclient.core.data.domain.UseCase
import me.stone.stanimeclient.core.data.local.AppPreferenceStorage
import me.stone.stanimeclient.core.model.DarkThemeMode
import javax.inject.Inject

open class SetDarkThemeModeCase @Inject constructor(
    private val appPreferenceStorage: AppPreferenceStorage,
    @IoDispatcher private val dispatcher: CoroutineDispatcher
) : UseCase<DarkThemeMode, Unit>(dispatcher) {
    override suspend fun execute(params: DarkThemeMode) {
        appPreferenceStorage.updateDarkThemeMode(params)
    }
}

open class GetDarkThemeModeCase @Inject constructor(
    private val appPreferenceStorage: AppPreferenceStorage,
    @IoDispatcher private val dispatcher: CoroutineDispatcher
) : UseCase<Unit, DarkThemeMode>(dispatcher) {
    override suspend fun execute(params: Unit): DarkThemeMode {
        return appPreferenceStorage.darkThemeMode.first()
    }
}

open class ObserveDarkThemeModeCase @Inject constructor(
    private val appPreferenceStorage: AppPreferenceStorage,
    @DefaultDispatcher private val dispatcher: CoroutineDispatcher
) : FlowUseCase<Unit, DarkThemeMode>(dispatcher) {
    override fun execute(params: Unit): Flow<Result<DarkThemeMode>> {
        return appPreferenceStorage.darkThemeMode.map {
            Result.Success(it)
        }
    }
}
